package com.tianpengtech.front.mobile.task;

import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.U;
import com.tianpengtech.front.mobile.common.util.UserUtil;
import com.tianpengtech.front.web.common.controller.BaseController;
import com.tianpengtech.front.web.login.LoginService;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ggd on 2017/7/28 0028.
 */
public class MobileTaskController extends BaseController {
    private static MobileTaskService service = MobileTaskService.me;
    /**
     * 获取任务广场任务列表
     */
    public void index(){
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",5);
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=service.taskIndex(pageNum,pageSize,user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 进行中和已完成任务查询
     */
    public void findTask(){
        int pageNum = getParaToInt("pageNum", 1);
        int pageSize = getParaToInt("pageSize",5);
        Map<String, Object> params = new HashMap<>();
        params.put("taskName", getPara("taskName"));
        params.put("taskStatus", getPara("status"));
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        params.put("user", user.get(User.ID));
        R r=service.findTask(pageNum,pageSize,params);
        renderJson(r);
    }

    /**
     * 获取任务详情
     */
    public void getTask(){
        String id = getPara("id");//任务ID
        if (!U.isInteger(id)){
            renderJson(R.fail("id格式不正确"));
            return;
        }
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=service.getTask(Long.parseLong(id),user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 接受任务
     */
    public void acceptTask(){
        String id = getPara("id");//任务ID
        if (!U.isInteger(id)){
            renderJson(R.fail("id格式不正确"));
            return;
        }
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=service.acceptTask(id,user.getLong(User.ID));
        renderJson(r);
    }
}
